﻿Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Content
Imports DotNetNuke.Entities.Content.Common
Imports DotNetNuke.Entities.Content.Taxonomy
Imports System.Linq

Public Class ContactContent
    Private Shared ReadOnly ContentTypeName As String = "GeminorumContactsContact"
    Private ReadOnly lContentItemController As iDotNetNukeContentItemController
    Private ReadOnly lContactTerms As iTerms(Of Contact)
    Private ReadOnly lContentTypeController As iDotNetNukeContentTypeController

#Region "---CreateContentItem---"
    Public Function CreateContentItem(Contact As Contact, tabId As Integer) As ContentItem
        Dim colContentTypes = From c In lContentTypeController.GetContentTypes Where c.ContentType = ContentTypeName
        Dim contentTypeId As Integer

        If colContentTypes.Count > 0 Then
            Dim contentType As ContentType = colContentTypes.Single
            If IsNothing(contentType) Then
                contentTypeId = CreateContentType()
            Else
                contentTypeId = contentType.ContentTypeId
            End If
        Else
            contentTypeId = CreateContentType()
        End If

        Dim objContent As New ContentItem
        objContent.Content = Contact.Name
        objContent.ContentTypeId = contentTypeId
        objContent.Indexed = False
        objContent.ContentKey = "tid=" & Contact.ID
        objContent.ModuleID = Contact.ModuleID
        objContent.TabID = tabId

        objContent.ContentItemId = lContentItemController.AddContentItem(objContent)

        lContactTerms.ManageContactTerms(Contact, objContent)

        Return objContent
    End Function
#End Region
#Region "---UpdateContentItem---"
    Public Sub UpdateContentItem(Contact As Contact, tabId As Integer)
        Dim objContent As ContentItem = lContentItemController.GetContentItem(Contact.ContentItemId)

        If IsNothing(objContent) Then
            Return
        End If

        objContent.Content = Contact.Name
        objContent.TabID = tabId
        lContentItemController.UpdateContentItem(objContent)

        lContactTerms.ManageContactTerms(Contact, objContent)
    End Sub
#End Region
#Region "---DeleteContentItem---"
    Public Sub DeleteContentItem(Contact As Contact)
        If Contact.ContentItemId = Null.NullInteger Then
            Exit Sub
        End If

        Dim objContent As ContentItem = lContentItemController.GetContentItem(Contact.ContentItemId)

        If IsNothing(objContent) Then
            Exit Sub
        End If

        lContactTerms.RemoveContactTerms(Contact)
        lContentItemController.DeleteContentItem(objContent)
    End Sub
#End Region

#Region "---CreateContentType---"
    Private Function CreateContentType() As Integer
        Dim objContentType As New ContentType
        objContentType.ContentType = ContentTypeName
        Return lContentTypeController.AddContentType(objContentType)
    End Function
#End Region

    Sub New(ContentItemController As iDotNetNukeContentItemController,
            ContactTerms As iTerms(Of Contact),
            ContentTypeController As iDotNetNukeContentTypeController)
        lContentItemController = ContentItemController
        lContactTerms = ContactTerms
        lContentTypeController = ContentTypeController
    End Sub
End Class